import { createCrudApi } from '@/utils/apiFactory';
import request from '@/utils/unirequest';
import type { IResult } from '@/type/common';
import type {
  ISysHrInterviewRequestBill,
  ISysHrInterviewRequestBillView,
  ISysHrInterviewRequestBillVm,
  IRecognizeIdentityCardRequest,
  IRecognizeIdentityCardResponse,
  IRecruitPublishRequestBillSearchQuery,
  IRecruitPublishRequestBillSearchResponse,
  // 子表相关接口类型
  ISubTableSearchRequest,
  IEducationResponse,
  IFamilyResponse,
  IWorkResponse,
  IHonorResponse,
  IEducationRequest,
  IFamilyRequest,
  IWorkRequest,
  IHonorRequest,
  // 人像验证接口类型
  IValidatePortraitRequest,
  IValidatePortraitResponse
} from './interface';

export default function SysHrInterviewRequestBillApi() {
  // 基础CRUD用工厂生成
  const baseCrud = createCrudApi<
    ISysHrInterviewRequestBillView,
    ISysHrInterviewRequestBillVm,
    ISysHrInterviewRequestBill
  >('/api/SysHrInterviewRequestBill');

  return {
    ...baseCrud,
    // 额外的特殊接口
    // 获取单据编号接口
    async getBillCode(): Promise<IResult<string>> {
      return await request.get('/api/SysHrInterviewRequestBill/PredictCode');
    },
    // 审核接口
    async approve(data: ISysHrInterviewRequestBillVm): Promise<ISysHrInterviewRequestBill> {
      return await request.post('/api/SysHrInterviewRequestBill/approve?tag=', data);
    },
    // 身份证识别接口
    async recognizeIdentityCard(data: IRecognizeIdentityCardRequest): Promise<IRecognizeIdentityCardResponse> {
      return await request.post('/api/_account/RecognizeIdentityCard', data);
    },
    // 搜索招聘发布单列表接口
    async searchRecruitPublishRequestBills(query: IRecruitPublishRequestBillSearchQuery): Promise<IRecruitPublishRequestBillSearchResponse> {
      return await request.post('/api/SysHrRecruitPublishRequestBill/Search', query);
    },

    // ===== 子表相关接口 =====

    // 学习经历相关接口
    async getEducationList(data: ISubTableSearchRequest): Promise<{ Data: IEducationResponse[] }> {
      return await request.post('/api/SysPersonEducation/Search', data);
    },
    async addEducation(data: IEducationRequest): Promise<any> {
      return await request.post('/api/SysPersonEducation/Add', data);
    },
    async deleteEducation(ids: string[]): Promise<any> {
      return await request.post('/api/SysPersonEducation/BatchDelete', ids);
    },

    // 家庭成员相关接口
    async getFamilyList(data: ISubTableSearchRequest): Promise<{ Data: IFamilyResponse[] }> {
      return await request.post('/api/SysPersonFamily/Search', data);
    },
    async addFamily(data: IFamilyRequest): Promise<any> {
      return await request.post('/api/SysPersonFamily/Add', data);
    },
    async deleteFamily(ids: string[]): Promise<any> {
      return await request.post('/api/SysPersonFamily/BatchDelete', ids);
    },

    // 工作经历相关接口
    async getWorkList(data: ISubTableSearchRequest): Promise<{ Data: IWorkResponse[] }> {
      return await request.post('/api/SysPersonWork/Search', data);
    },
    async addWork(data: IWorkRequest): Promise<any> {
      return await request.post('/api/SysPersonWork/Add', data);
    },
    async deleteWork(ids: string[]): Promise<any> {
      return await request.post('/api/SysPersonWork/BatchDelete', ids);
    },

    // 荣誉职称相关接口
    async getHonorList(data: ISubTableSearchRequest): Promise<{ Data: IHonorResponse[] }> {
      return await request.post('/api/SysPersonProfessionalTitle/Search', data);
    },
    async addHonor(data: IHonorRequest): Promise<any> {
      return await request.post('/api/SysPersonProfessionalTitle/Add', data);
    },
    async deleteHonor(ids: string[]): Promise<any> {
      return await request.post('/api/SysPersonProfessionalTitle/BatchDelete', ids);
    },

    // 文件预览接口
    async previewFile(fileId: string, fileName?: string): Promise<void> {
      const url = `/api/_file/getfile/${fileId}`;
      if (fileName) {
        // 如果提供了文件名，可以在这里处理文件下载/预览逻辑
        console.log(`预览文件: ${fileName}, URL: ${url}`);
      }
      // 这里可以添加具体的文件预览实现
      uni.downloadFile({
        url: `${import.meta.env.VITE_BASE_URL}${url}`,
        success: (res) => {
          if (res.statusCode === 200) {
            uni.openDocument({
              filePath: res.tempFilePath,
              success: () => {
                console.log('文件打开成功');
              },
              fail: (err) => {
                console.error('文件打开失败:', err);
                uni.showToast({
                  title: '文件打开失败',
                  icon: 'none'
                });
              }
            });
          }
        },
        fail: (err) => {
          console.error('文件下载失败:', err);
          uni.showToast({
            title: '文件下载失败',
            icon: 'none'
          });
        }
      });
    },

    // 获取资质类别
    async getProfessionalTitleTypes(): Promise<{ Value: string; Text: string }[]> {
      return await request.get('/api/SysPersonProfessionalTitle/GetSysPersonProfessionalTitleTypes');
    },

    // 获取部门组织架构
    async getDepartmentTree(): Promise<any[]> {
      return await request.get('/api/SysGroup/GetParentsTree');
    },

    // 根据部门获取岗位
    async getPositionsByDepartment(departmentId: string): Promise<{ Value: string; Text: string }[]> {
      return await request.get(`/api/SysJobPosition/GetByGroupId/${departmentId}`);
    },

    // 人像验证接口
    async validatePortrait(data: IValidatePortraitRequest): Promise<IValidatePortraitResponse> {
      return await request.post('/api/_account/GenerateIdPhoto', data);
    },
  };
}
